package net.rock.controller;

import net.rock.pojo.Address;
import net.rock.service.AddressService;
import net.rock.utils.ServiceCaller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;



@RestController
@RequestMapping("/api/address")
public class AddressController {
	@Autowired
    private ServiceCaller serviceCaller;

    @Autowired
    private AddressService addressService;
    

    @GetMapping("/show")
    public List<Address> showAddress(@CookieValue("sessionID")String sessionId) {
        int userId = serviceCaller.fetchUserBySessionId(sessionId).getId();
        return addressService.showAddress(userId);
    }

    @PostMapping("/add")
    public void add(@CookieValue(value = "sessionID") String sessionId,@RequestBody Address address){
        int userId = serviceCaller.fetchUserBySessionId(sessionId).getId();
        address.setUserId(userId);
        addressService.add(address);
    }

    @PostMapping("/del")
    public void del(@CookieValue(value = "sessionID") String sessionId,@RequestBody Address address){
        int userId = serviceCaller.fetchUserBySessionId(sessionId).getId();
        address.setUserId(userId);
        addressService.del(address);
    }

    @PostMapping("/edit")
    public void edit(@CookieValue(value = "sessionID") String sessionId,@RequestBody Address address){
        System.out.printf("Edit %s\n", address);
        int userId = serviceCaller.fetchUserBySessionId(sessionId).getId();
        address.setUserId(userId);
        addressService.edit(address);
    }
}